local skynet = require "skynet"
local skynet_managet = require "skynet.manager"
local log = require "sky_log"
local uu = require "sky_utils"
--config
local env = require "env.env" 
local room_env_config =  require ("config.room_env_config"..(env.env)) --0 1 2 10 12

local service_room = {}

function service_room:gen_table_id()
    local table_id = ''
    table_id = table_id .. self.room_config.game_id .. "-".. self.room_config.room_id.."-".. self.table_index
  
    return table_id
end

function service_room:create_table()
    local table_id = self:gen_table_id()
    self.table_list[table_id] = {}
    self.table_list[table_id].table_id = table_id
    local table = skynet.newservice("service_table")
    self.table_list[table_id].table_agent = table
    self.table_list[table_id].player_ids = {}
    self.table_list[table_id].table_index = self.table_index
    self.table_list[table_id].create_time = os.time()
    --self.table_list[table_id].room_config =  self.room_config
    self.table_list[table_id].room_level =  self.room_level
    self.table_list[table_id].room_id =  self.room_id

    skynet.send(table, "lua", "init_table", self.table_list[table_id], self.room_config) 

    --每个房间容量设计为10万桌子
    self.table_index = self.table_index + 1
    if self.table_index > 100000 then
        self.table_index = 1
    end
    return table_id
end

function service_room:init_room(room_config)
    log.debug("room_config=", room_config)
    self.room_config = room_config
    self.room_id = room_config.room_id
    self.room_level = room_config.room_level
    self.game_type = room_config.game_type or 25

    self.table_list = {}
    self.table_index = 1
    for i=1, 1 do
        local table_id = self:create_table()
    end
    log.debug("self.table_list=", self.table_list)
end

function service_room:enter_room(agent, player_info)
    local info = {}
    info.login_res = {
        room_info = {
            room_base_note = tostring(self.room_config.room_base_note),
            room_min_entrance = tostring(self.room_config.room_min_entrance)
        },
        player_info = player_info
    }
    info.player_id = player_info.player_id
    info.room_id = self.room_id

    skynet.send(agent, "lua", "login_in", skynet.self(), info, 20)

    local select = room_env_config.login_server_url.select[player_info.hall_index]
    local host = room_env_config.login_server_url.server[select]
    local url_info = {
        uin = player_info.player_id,
        game_type = self.game_type,
        room_level = self.room_level,
        room_id = self.room_id,
    }
    uu.http_request(host, "report_enter_room", url_info)
end

function service_room:enter_game(agent, player_info, table_id)
    if not table_id then 
        table_id = self:create_table()
    end

    --选择桌子逻辑
    local table_agent = self.table_list[table_id].table_agent
    self.table_list[table_id].player_ids[player_info.player_id] = true
    log.debug("self.table_list=", self.table_list)

    skynet.send(table_agent, "lua", "enter_game", agent, player_info)

    local select = room_env_config.login_server_url.select[player_info.hall_index]
    local host = room_env_config.login_server_url.server[select]
    local url_info = {
        uin = player_info.player_id,
        game_type = self.game_type,
        room_level = self.room_level,
        room_id = self.room_id,
        table_id = table_id,
    }
    uu.http_request(host, "report_enter_game", url_info)
end

function service_room:disconnect(player_id, table_id)
    if not table_id then 
        log.error("disconnect no table_id!")
        return
    end
    self.table_list[table_id].player_ids[player_id] = nil
    log.debug("self.player_id=", player_id)
    log.debug("self.table_id=", table_id)
    log.debug("self.table_list=", self.table_list)
    skynet.send(self.table_list[table_id].table_agent, "lua", "disconnect", player_id)
end

--start
skynet.start(function()
    --session是当前服务地址  address是源服务(发消息来的服务)地址
    skynet.dispatch("lua", function(session, address, cmd, ...)
        --log.info("service_room dispatch cmd=", cmd)
        --log.info("service_room dispatch session=", session)
        --log.info("service_room dispatch address=", address)
        local f = service_room[cmd]
        if f then  
            skynet.retpack(f(service_room, ...))
        else
            log.error(string.format("Unknown command %s", tostring(cmd)))
        end
    end)
end)

return service_room